#include "54func.h"
#include "clientFunc.h"

//全局变量
int loginFlag;

int main(int argc, char *argv[])
{
    // ./client ip port
    // TCP通信
    ARGS_CHECK(argc,3);
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    struct sockaddr_in serverAddr;
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(atoi(argv[2]));
    serverAddr.sin_addr.s_addr = inet_addr(argv[1]);
    int ret = connect(sockfd,(struct sockaddr *)&serverAddr,sizeof(serverAddr));
    ERROR_CHECK(ret,-1,"connect");
    int epfd = epoll_create(1);
    struct epoll_event events;
    
    events.events = EPOLLIN;
    events.data.fd = STDIN_FILENO;
    epoll_ctl(epfd,EPOLL_CTL_ADD, STDIN_FILENO, &events);

    //初始化需要的参数
    cmd_t cmd;
    char buf[4096] = {0};

    //开始业务
    while(1){
        struct epoll_event readySet[1];
        epoll_wait(epfd,readySet,1024,-1);
        if(readySet[0].data.fd == STDIN_FILENO){
            fgets(buf,sizeof(buf),stdin);
            if(parseToken(sockfd,buf,&cmd) == 0) {
                cmdClientHandler(sockfd, &cmd);
            }  
        }
    }
    return 0;
}
